﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Microsoft.TV.TVControls;
using IPTV.service;
using IPTV.module;
using IPTV.util;
using System.Threading;
using Microsoft.TV.TVControls.Actions;
using System.Xml;

public partial class page_primaryEnglish : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.HasKeys() && Request.QueryString["sel"] != null)
        {
            string sel = Request.QueryString["sel"].ToString().Trim();
            if (sel.Equals("free"))
            {
                TVMenu1.InitialSelectedMenuItem = "TVMenuItem1";
            }
            else if (sel.Equals("playlog"))
            {
                TVMenu1.InitialSelectedMenuItem = "TVMenuItem2";
            }
            else if (sel.Equals("activity"))
            {
                TVMenu1.InitialSelectedMenuItem = "TVMenuItem3";
            }
            else if (sel.Equals("pay"))
            {
                TVMenu1.InitialSelectedMenuItem = "TVMenuItem4";
            }
        }

        ActivityService atService = new ActivityService();
        List<CourseDto> freeCourseList = atService.getFreeCourse();
        freeToXML(Request.PhysicalApplicationPath + "Data\\activity\\", "free", freeCourseList);
        if (null == freeCourseList || freeCourseList.Count == 0)
        {
            TVPage1.Controls.Remove(shikanPanel);
            TVPage1.Controls.Add(CommonUtil.getNoDataTVVerticalFlowPanel("no_free_panel", false));
            TVMenuItem1.SubMenuID = "no_free_panel";
        }

        List<CourseDto> playLogList = atService.getPlayLog("fww");
        playLogToXML(Request.PhysicalApplicationPath + "Data\\activity\\", "playLog", playLogList);
        if (null == playLogList || playLogList.Count == 0)
        {
            TVPage1.Controls.Remove(logPanel);
            TVPage1.Controls.Add(CommonUtil.getNoDataTVVerticalFlowPanel("no_playLog_panel", false));
            TVMenuItem2.SubMenuID = "no_playLog_panel";
        }


        List<CourseDto> adList = atService.getAD();
        adToXML(Request.PhysicalApplicationPath + "Data\\activity\\", "ad", adList);
        if (null == adList || adList.Count == 0)
        {
            TVPage1.Controls.Remove(hdPanel);
            TVPage1.Controls.Add(CommonUtil.getNoDataTVVerticalFlowPanel("no_ad_panel", false));
            TVMenuItem3.SubMenuID = "no_ad_panel";
        }

        zfLbl.IsCDataText = true;
        zfLbl.Text = "产品说明:小学培优，是三及第互动教育精心打造的产品之一。本产品内容覆盖小学各个年级（小一至"
            + "小六）的语文、数学、英语三大基础科目。主要以优秀主题为主，每个专题大概5-10条高清视频课程。\n"
        + "产品价格：推广价20元/月（原价38元），付费后可以观看本频道所有内容。注意：用户订购后，系统每月会自动扣费。\n"
        + "如果您在使用的过程中，遇到困难或者有任何疑问，请拨打客服热线进行咨询，我们将热情为您服务。客服热线：969368。";
         
    }

    private void freeToXML(string path, string fileName, List<CourseDto> courseList)
    {
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(dec);
        XmlElement root = doc.CreateElement("listitems");
        doc.AppendChild(root);
        XmlNode rowNode = doc.CreateElement("Row");
        root.AppendChild(rowNode);

        try
        {

            foreach (CourseDto course in courseList)
            {
                XmlElement entry = doc.CreateElement("Entry");
                entry.SetAttribute("url", "");
                entry.SetAttribute("title", CommonUtil.subEllipsisText(course.VideoName, 30));
                entry.SetAttribute("detail", "授课老师:" + course.TeacherName + "\n" + "课程时长:" + course.VideoLength + "\n" + "课程介绍:" + CommonUtil.replaceLineText(course.Remark));
                entry.SetAttribute("imgUrl", course.SmallUrl);
                rowNode.AppendChild(entry);
            }

            doc.Save(path + "\\" + fileName + ".xml");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }


    }


    private void playLogToXML(string path, string fileName, List<CourseDto> courseList)
    {
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(dec);
        XmlElement root = doc.CreateElement("listitems");
        doc.AppendChild(root);
        XmlNode rowNode = doc.CreateElement("Row");
        root.AppendChild(rowNode);

        try
        {

            foreach (CourseDto course in courseList)
            {
                XmlElement entry = doc.CreateElement("Entry");
                entry.SetAttribute("url", "");
                entry.SetAttribute("title", CommonUtil.subEllipsisText("【"+course.SubjectName+"】"+course.VideoName+" "+course.CreateTime, 50));
                entry.SetAttribute("detail", "授课老师:" + course.TeacherName + "\n" + "课程时长:" + course.VideoLength + "\n" + "课程介绍:" + CommonUtil.replaceLineText(course.Remark));
                entry.SetAttribute("imgUrl", course.SmallUrl);
                rowNode.AppendChild(entry);
            }

            doc.Save(path + "\\" + fileName + ".xml");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    private void adToXML(string path, string fileName, List<CourseDto> courseList)
    {
        XmlDocument doc = new XmlDocument();
        XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(dec);
        XmlElement root = doc.CreateElement("listitems");
        doc.AppendChild(root);
        XmlNode rowNode = doc.CreateElement("Row");
        root.AppendChild(rowNode);
        try
        {
            foreach (CourseDto course in courseList)
            {
                XmlElement entry = doc.CreateElement("Entry");
                entry.SetAttribute("url", "");
                entry.SetAttribute("imgUrl", course.SmallUrl);
                rowNode.AppendChild(entry);
            }

            doc.Save(path + "\\" + fileName + ".xml");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}